package it.prova.xml;

import it.prova.Consts;
import it.prova.MyModelContent;
import it.prova.Utility;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Serializer;

public class MyOutputSerializer extends Serializer
{
//	private Document model;
	
	private int unitType;
	
	private Map<String, MyModelContent> modelContent;
	
	private String pageId;
	private String unitId;
	
	public MyOutputSerializer(OutputStream out, Document model)
	{
		super(out);
		
//		this.model = model;
		this.modelContent = Utility.getModelContent(model);
		this.unitId = Utility.getUnitId(model);
		this.unitType = Utility.getUnitType(model);
		
		this.pageId = Utility.getPageId(Consts.ENTRY_UNIT);
		String[] pageIdValueSplitted = pageId.split("#");
		this.pageId = pageIdValueSplitted[pageIdValueSplitted.length-1];
	}
	
	@Override
	protected void writeXMLDeclaration() throws IOException
	{
		// do nothing
//		super.writeXMLDeclaration();
	}

	@Override
	protected void write(Element element) throws IOException
	{
		String namespaceURI = element.getNamespaceURI();
		
	    if(Consts.TAG_BODY_NAME.equals(element.getLocalName()))
		{
			writeRaw("<wr:Frame>");
			for(int i=0; i<element.getChildCount(); i++)
			{
				super.writeChild(element.getChild(i));
			}
			writeRaw("</wr:Frame>");
		}
		else if
		(
			namespaceURI != null && 
			!"".equals(namespaceURI) && 
			!"null".equals(namespaceURI)
		)
		{
			if
			(
				Consts.URI_DATAUNIT.equals(namespaceURI) || 
				Consts.URI_MULTIDATAUNIT.equals(namespaceURI)
			)
			{
				if(Consts.TAG_LABEL_NAME.equals(element.getLocalName()))
				{
					String varAttributeValue = element.getAttributeValue(Consts.MODEL_ATTRIBUTE_NAME);
					if(varAttributeValue != null)
					{
						String content = modelContent.get(varAttributeValue).getLabelContent();
						if(content != null)
						{
							content = content.replaceAll(Consts.WR_PLACE_HOLDER_ESCAPED, varAttributeValue);
//							content = content.replaceAll(Consts.WR_PLACE_HOLDER_ESCAPED, Consts.WR_VALUE_LABEL_PLACE_HOLDER);
							writeRaw(content);
						}
					}
//					writeRaw("***LABEL***");
				}
				else if(Consts.TAG_VALUE_NAME.equals(element.getLocalName()))
				{
					String varAttributeValue = element.getAttributeValue(Consts.MODEL_ATTRIBUTE_NAME);
					if(varAttributeValue != null)
					{
						String content = modelContent.get(varAttributeValue).getValueContent();
						if(content != null)
						{
							switch(unitType)
							{
								case Consts.DATA_UNIT:
									content = content.replaceAll(Consts.WR_PLACE_HOLDER_ESCAPED, "<c:out value=\"\\${" + unitId + ".data." + varAttributeValue + "}\"/>");
									break;
								case Consts.MULTI_DATA_UNIT:
									content = content.replaceAll(Consts.WR_PLACE_HOLDER_ESCAPED, "<c:out value=\"\\${current." + varAttributeValue + "}\"/>");
									break;
							}
//							content = content.replaceAll(Consts.WR_PLACE_HOLDER_ESCAPED, Consts.WR_VALUE_VALUE_PLACE_HOLDER);
							writeRaw(content);
						}
					}
//					writeRaw("***VALUE***");
				}
				else if(Consts.TAG_ITERATE_NAME.equals(element.getLocalName()))
				{
					writeRaw("<c:forEach var=\"current\" varStatus=\"status\" items=\"${"+ unitId + ".data}\">");
					writeRaw("<c:set var=\"index\" value=\"${status.index}\" />");
					for(int i=0; i<element.getChildCount(); i++)
					{
						super.writeChild(element.getChild(i));
					}
					writeRaw("</c:forEach>");
				}
			}
			else if
			(
				Consts.URI_ENTRYUNIT.equals(namespaceURI)
			)
			{
				if(Consts.TAG_LABEL_NAME.equals(element.getLocalName()))
				{
					String fieldName = Utility.getEntryUnitFieldName(element.getAttributeValue(Consts.MODEL_ATTRIBUTE_NAME));
					
					if(fieldName != null)
					{
						writeRaw(fieldName);
					}
				}
				else if(Consts.TAG_VALUE_NAME.equals(element.getLocalName()))
				{
					String typeAttributeValue = element.getAttributeValue(Consts.MODEL_ATTRIBUTE_TYPE);
					String nameAttributeValue = element.getAttributeValue(Consts.MODEL_ATTRIBUTE_NAME);
					
//					String cls = null;
					if(nameAttributeValue != null)
					{
						String[] nameAttributeIdValueSplitted = nameAttributeValue.split("#");
						String fieldId = nameAttributeIdValueSplitted[nameAttributeIdValueSplitted.length-1];
						
						if(fieldId != null && pageId != null)
						{
							if(Consts.FIELD_TYPE_FIELD.equals(typeAttributeValue))
							{
		//						cls = "field";
								String fieldType = Utility.getEntryUnitFieldType(nameAttributeValue);
								
								if(fieldType != null)
								{
									if(Consts.ATTRIBUTE_TYPE_PASSWORD.equals(fieldType))
									{
										writeRaw("<html:password onkeypress=\"if(event.keyCode==13){clickButton('ln1'); return false;}\" styleId=\"" + fieldId + "\" size=\"25\" styleClass=\" field\" property=\"" + fieldId + "\" readonly=\"false\" redisplay=\"false\"/>");
									}
									else
									{
										writeRaw("<html:text onkeypress=\"if(event.keyCode==13){clickButton('ln1'); return false;}\" size=\"25\" styleId=\"" + fieldId + "\" styleClass=\" field\" property=\"" + fieldId + "\" readonly=\"false\"/>");
									}
								}
							}
							else if(Consts.FIELD_TYPE_MULTI_SELECTION_FIELD.equals(typeAttributeValue))
							{
		//						cls = "multiselectionfield";
								writeRaw("<c:forEach var=\"current\" varStatus=\"status\" items=\"${" + pageId + "FormBean.map.ms" + fieldId + "List}\">");
								writeRaw("<html:multibox onkeypress=\"if(event.keyCode==13){clickButton('ln1'); return false;}\" styleClass=\" multiselectionfield\" property=\"ms" + fieldId + "\" value=\"${current}\" disabled=\"false\"/>");
								writeRaw("<c:out value=\"${" + pageId + "FormBean.map.ms" + fieldId + "LabelList[status.index]}\"/>");
								writeRaw("</c:forEach>");
							}
							else if(Consts.FIELD_TYPE_SELECTION_FIELD.equals(typeAttributeValue))
							{
		//						cls = "selectionfield";
								writeRaw("<html:select onkeypress=\"if(event.keyCode==13){clickButton('ln1'); return false;}\" styleId=\"" + fieldId + "\" styleClass=\" selectionfield\" property=\"" + fieldId + "\" disabled=\"false\">");
								writeRaw("<html:option value=\"\">");
								writeRaw("<bean:message key=\"selectionfield.noselection\"/>");
								writeRaw("</html:option>");
								writeRaw("<html:options property=\"" + fieldId + "List\" labelProperty=\"" + fieldId + "LabelList\"/>");
								writeRaw("</html:select>");
							}
						}
					}
//					writeRaw("<wr:Value class=\"" + cls + "\"/>");
				}
				else if(Consts.TAG_LINK_NAME.equals(element.getLocalName()))
				{
					String value = element.getAttributeValue(Consts.TAG_ATTRIBUTE_VALUE);
					writeRaw("<input title=\"" + value + "\" onclick=\"$('" + pageId + "FormBean').target='_self'\" class=\" button\"  id=\"button#ln1\" name=\"button#ln1\" type=\"submit\" value=\"" + value + "\">");
				}
			}
		}
		else
		{
			super.write(element);
		}
	}	
}